package applet.attendee.locked.components.schedule;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import applet.misc.graphics.AttendeeBackgroundPanel;

import data.CMSRes;
import data.account.attendee.AttendeeAccount;
import data.event.Event;
import data.event.EventTable;

public class ScheduleDialog extends JDialog
{
	private static final long serialVersionUID = 5234573088854146195L;
	
	private ArrayList<Event> events;
	
	private EventTable eventsTable;
	private JTextArea descriptionArea;

	public ScheduleDialog(AttendeeAccount attendee)
	{
		super(CMSRes.frame(), "Events Schedule", true);

		defineComponents(attendee);

		setSize(600, 300);
		setResizable(false);

		setLayout(new BorderLayout());
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		JPanel layoutHelper = new AttendeeBackgroundPanel(new BorderLayout());
				JPanel layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 10));
				layout2.setOpaque(false);
				layout2.add(new JScrollPane(eventsTable), BorderLayout.CENTER);
			layoutHelper.add(layout2, BorderLayout.CENTER);
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 5, 10, 10));
				layout2.setOpaque(false);
				layout2.add(new JScrollPane(descriptionArea), BorderLayout.CENTER);
			layoutHelper.add(layout2, BorderLayout.EAST);
		add(layoutHelper, BorderLayout.CENTER);
		
		pack();
		validate();
	}
	
	private void defineComponents(AttendeeAccount attendee)
	{
		descriptionArea = new JTextArea("(no event selected)");
		descriptionArea.setEditable(false);
		descriptionArea.setLineWrap(true);
		descriptionArea.setWrapStyleWord(true);
		descriptionArea.setPreferredSize(new Dimension(350, 300));
		descriptionArea.setSize(350, 300);
		
		events = new ArrayList<Event>();
		for(Event event : CMSRes.conference().events())
			if(event.attendees().contains(attendee.userID()))
				events.add(event);
		Collections.sort(events);
		eventsTable = new EventTable(events);
		eventsTable.setFillsViewportHeight(true);
		eventsTable.setSize(350, 300);
		eventsTable.setPreferredSize(new Dimension(350, 300));
		EventSelectListener listener = new EventSelectListener();
		eventsTable.getSelectionModel().addListSelectionListener(listener);
		eventsTable.getColumnModel().getSelectionModel().addListSelectionListener(listener);
	}
	
	private class EventSelectListener implements ListSelectionListener 
	{
		@Override public void valueChanged(ListSelectionEvent e)
		{
			Event event = eventsTable.getSelected();
			if(event != null)
			{
				descriptionArea.setText(event.description());
				descriptionArea.validate();
				descriptionArea.repaint();
			}
		}
	}
}